/**
 * 
 */
package soa.security.web.action;

import org.apache.commons.lang3.StringUtils;

import soa.security.domain.Region;
import soa.security.web.support.action.LdapModelAction;

/**
 * @author Cre.Gu
 *
 */
public class RegionAction extends LdapModelAction<Region> {

	private static final long serialVersionUID = -8925615466644106335L;

	public String list() {
		return SUCCESS;
	}

	public String grid() {
		updateResultContent(regionMgrService.allRegions(parentDn,
				buildContext().getParameters()));
		return SUCCESS;
	}

	public String edit() {
		Region region = getModel();
		if (StringUtils.isBlank(region.getBid())) {
			region.setBid("RE" + positionMgrService.findMaxNumber(Region.class));
		}
		region.setParentDn(parentDn);

		updateResultContent(region);

		this.pageResult.addData("parentName", getRegion(parentDn).getName());
		return SUCCESS;
	}

	public String doneSave() {
		regionMgrService.addRegion(parentDn, getModel());

		return SUCCESS;
	}

	public String doneUpdate() {
		regionMgrService.updateRegion(parentDn, getModel());
		return SUCCESS;
	}

	public String doneValidate() throws Exception {
		Region a = getModel();

		boolean flag = regionMgrService.existCode(dnp, a.getCode());
		if (flag) {
			Region exist = regionMgrService.existByCode(a.getCode());
			if (dnp.equals(exist.getDn().toString()))
				return SUCCESS;

			String n = exist != null && exist.getName() != null ? exist
					.getName() : "";
			this.pageResult.addData("existDn", exist.getDn().toString());
			throw new Exception("行政区划代码已经被" + n + "使用");
		}
		return SUCCESS;
	}

	public String add() {
		Region region = getModel();

		updateResultContent(region);
		return SUCCESS;
	}

	@Override
	protected Region findByDn(String dn) {
		return regionMgrService.findByDn(dn);
	}
}
